/*
    XTU OJ: 1140
    by: fifth_light
*/

#include <stdio.h>
#include <inttypes.h>
#include <stdint.h>
#include <assert.h>

int64_t longPow(int base, int power) {
    int64_t result = 1;
    for (int i = 0; i < power; i++) {
        result *= base;
    }
    return result;
}

int main(void) {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        char str[1024];
        int l1, l2, l;
        if (scanf(" %n%1023[01-]%n", &l1, str, &l2) <= 0) break;
        l = l2 - l1;
        
        int64_t result = 0;
        for (int i = 0; i < l; i++) {
            int str_index = l - i - 1;
            char c = str[str_index];
            int value;
            switch (c) {
                case '0':
                    value = 0;
                    break;
                case '1':
                    value = 1;
                    break;
                case '-':
                    value = -1;
                    break;
                default:
                    assert(0);
            }
            result += longPow(3, i) * value;
        }
        printf("%" PRId64 "\n", result);
    }
    return 0;
}
